/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.gateway.server.listener;

import com.iwindplus.boot.domain.dto.MessageDTO;
import com.iwindplus.gateway.server.filter.ApiWhiteListFilter;
import com.iwindplus.mgt.domain.constant.MgtConstant;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * API白名单更新监听.
 *
 * @author zengdegui
 * @since 2024-8-26
 */
@Slf4j
@Component
public class ApiWhiteListListener implements ApplicationRunner {
    @Resource
    private RedissonClient redissonClient;

    @Lazy
    @Resource
    private ApiWhiteListFilter apiWhiteListFilter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        final RTopic rTopic = this.redissonClient.getTopic(MgtConstant.TOPIC_API_WHITE_LIST);
        rTopic.addListener(MessageDTO.class, (channel, msg) -> {
            log.info("API白名单消息消费开始，数据={}", msg);
            this.apiWhiteListFilter.refreshWhiteList();
            log.info("API白名单消息消费成功");
        });
    }
}
